import java.util.Scanner;

public class XiangQi {
    /*
    使用说明：
    1、程序启动后会显示初始棋盘
    2、红方和黑方轮流走棋
    3、输入格式示例：‘车00到05’（表示将车从（0,0）移动到（0,5））
    4、输入“退出”可以结束游戏

    功能说明：
    实现了基本的棋盘显示
    支持红黑双方轮流走棋
    简单的移动验证（实际象棋规则更复杂）
    控制台界面操作

    改进方向：
    这只是一个基础实现，可以进一步扩展：
    1、实现每种棋子的特定移动规则
    2、添加将军和将死的判断
    3、增肌悔棋功能
    4、实现图形界面（Swing/JavaFX）
    5、添加网络对战功能
     */

    //棋盘表示
    private static final int BOARD_SIZE=10;//板面大小
    private static final int EMPTY=0;//空的
    private static final int RED=1;//红棋
    private static final int BLACK=2;//黑棋

    //棋子类型
    private static final int GENERAL=1;//将/帅
    private static final int ADVISOR=2;//士
    private static final int ELEPHANT=3;//象/相
    private static final int HORSE=4;//马
    private static final int CHARIOT=5;//车
    private static final int CANNON=6;//炮
    private static final int SOLDER=7;//兵/卒

    //期盼数组，第一个维度表示棋盘类型，第二个维度表示阵营
    private static int[][][] board=new int[BOARD_SIZE][BOARD_SIZE][2];

    //当前玩家
    private static int currentPlayer=RED;

    public static void main(String[] args) {
        initializeBoard();
        printBoard();
        Scanner scanner=new Scanner(System.in);
        while(true){
            System.out.println(currentPlayer==RED?"红方回合":"黑方回合");
            System.out.println("请输入移动指令（例如车2 5到2 7）");
            String input=scanner.nextLine();
            if(input.equals("退出")){
                break;
            }try {
                String[] parts=input.split("");
                String pieceName=parts[0];
                int fromX=Integer.parseInt(parts[1]);
                int fromY=Integer.parseInt(parts[2]);
                int toX=Integer.parseInt(parts[4]);
                int toY=Integer.parseInt(parts[5]);

                if(movePiece(pieceName,fromX,fromY,toX,toY)){
                    printBoard();
                    currentPlayer=(currentPlayer==RED)?RED:BLACK;
                }else{
                    System.out.println("无效移动，请重新尝试！");
                }
            }catch (Exception e){
                System.out.println("输入格式错误，请按照示例的格式输入！");
            }
        }
        scanner.close();
    }

    //初始化棋盘
    private static void initializeBoard(){
        //初始化红方
        board[0][0]=new int[]{CHARIOT,RED};
        board[0][1]=new int[]{HORSE,RED};
        board[0][2]=new int[]{ELEPHANT,RED};
        board[0][3]=new int[]{ADVISOR,RED};
        board[0][4]=new int[]{GENERAL,RED};
        board[0][5]=new int[]{ADVISOR,RED};
        board[0][6]=new int[]{ELEPHANT,RED};
        board[0][7]=new int[]{HORSE,RED};
        board[0][8]=new int[]{CHARIOT,RED};
        board[2][1]=new int[]{CANNON,RED};
        board[2][7]=new int[]{CANNON,RED};
        board[3][0]=new int[]{SOLDER,RED};
        board[3][2]=new int[]{SOLDER,RED};
        board[3][4]=new int[]{SOLDER,RED};
        board[3][6]=new int[]{SOLDER,RED};
        board[3][8]=new int[]{SOLDER,RED};

        //初始化黑方（上方）
        board[9][0]=new int[]{CHARIOT,BLACK};
        board[9][1]=new int[]{HORSE,BLACK};
        board[9][2]=new int[]{ELEPHANT,BLACK};
        board[9][3]=new int[]{ADVISOR,BLACK};
        board[9][4]=new int[]{GENERAL,BLACK};
        board[9][5]=new int[]{ADVISOR,BLACK};
        board[9][6]=new int[]{ELEPHANT,BLACK};
        board[9][7]=new int[]{HORSE,BLACK};
        board[9][8]=new int[]{CHARIOT,BLACK};
        board[7][1]=new int[]{CANNON,BLACK};
        board[7][7]=new int[]{CANNON,BLACK};
        board[6][0]=new int[]{SOLDER,BLACK};
        board[6][2]=new int[]{SOLDER,BLACK};
        board[6][4]=new int[]{SOLDER,BLACK};
        board[6][6]=new int[]{SOLDER,BLACK};
        board[6][8]=new int[]{SOLDER,BLACK};
    }


//移动棋子
    private static boolean movePiece(String pieceName, int fromX, int fromY, int toX, int toY) {
        //检查坐标是否在棋盘内
        if(fromX<0||fromX>=BOARD_SIZE||fromY<0||fromY>=BOARD_SIZE||toX<0||toX>=BOARD_SIZE||toY<0||toY>=BOARD_SIZE){
            return false;
        }

        //检查起始位置是否有棋子
        if(board[fromX][fromY][0]==EMPTY){
            return false;
        }

        //检查是否是当前玩家的棋子
        if(board[fromX][fromY][1]!=currentPlayer){
            return false;
        }

        //检查棋子名称是否匹配
        int pieceType=getPieceTypeByName(pieceName);
            if(pieceType==-1||board[fromX][fromY][0]!=pieceType){
                return false;
            }

        //简单的移动逻辑
        //这里只是演示，实际应该实现每种棋子的特定移动规则
        //检查目标位置是否是自己的棋子
        if(board[toX][toY][0]!=EMPTY&&board[toX][toY][1]==currentPlayer){
            return false;
        }

        //执行移动
        board[toX][toY]=board[fromX][fromY];
        board[fromX][fromY]=new int[]{EMPTY,0};
        return true;
    }

    //根据棋子名称获取棋子类型
    private static int getPieceTypeByName(String name){
        switch (name){
            case "将": case "帅":return GENERAL;
            case "士": case "仕":return ADVISOR;
            case "象": case "相":return ELEPHANT;
            case "马":return HORSE;
            case "车":return CHARIOT;
            case "炮":return CANNON;
            case "兵": case "卒":return SOLDER;
            default:return -1;
        }
    }


//打印棋盘
    private static void printBoard() {
        System.out.println("0 1 2 3 4 5 6 7 8");
        for (int i = 0; i<BOARD_SIZE ; i++) {
            System.out.println(i+"");
            for(int j=0;j<BOARD_SIZE;j++){
                if(board[i][j][0]==EMPTY){
                    System.out.println(".");
                }else{
                    int piece=board[i][j][0];
                    int color=board[i][j][1];

                    String pieceChar="";
                    switch (piece){
                        case GENERAL -> {
                            pieceChar=color==RED?"帅":"将";
                            break;
                        }
                        case ADVISOR -> {
                            pieceChar=color==RED?"仕":"士";
                            break;
                        }
                        case ELEPHANT -> {
                            pieceChar=color==RED?"相":"象";
                            break;
                        }
                        case HORSE -> {
                            pieceChar=color==RED?"马":"马";
                            break;
                        }
                        case CHARIOT -> {
                            pieceChar=color==RED?"车":"车";
                            break;
                        }
                        case CANNON -> {
                            pieceChar=color==RED?"炮":"炮";
                            break;
                        }
                        case SOLDER -> {
                            pieceChar=color==RED?"兵":"卒";
                            break;
                        }
                    }
                    System.out.println(pieceChar+"");
                }
            }
            System.out.println();
        }
    }
}